package com.petalsgroup.petals.service.animation.impl;

import com.petalsgroup.petals.dao.Animation.AnimationDao;
import com.petalsgroup.petals.dao.Animation.impl.AnimationDaoImpl;
import com.petalsgroup.petals.dao.Comment.CommentDao;
import com.petalsgroup.petals.dao.Comment.Impl.CommentDaoImpl;
import com.petalsgroup.petals.dao.collect.CollectDao;
import com.petalsgroup.petals.dao.collect.impl.CollectDaoImpl;
import com.petalsgroup.petals.pojo.*;
import com.petalsgroup.petals.service.animation.AnimationService;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.util.RecentlyUtil;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AnimationServiceImpl implements AnimationService {
    private final AnimationDao animationDao=new AnimationDaoImpl();
    private CommentDao commentDao = new CommentDaoImpl();
    private CollectDao collectDao = new CollectDaoImpl();
    private AnimationDetailedVO aDV=new AnimationDetailedVO();
    private AnimationSimpleDO aSD=new AnimationSimpleDO();
    private AnimationDetailedDO aDD=new AnimationDetailedDO();
    private CategoryDO cD=new CategoryDO();
    private List<CommentsDO> list=new ArrayList<>();
    private List<AnimationSimpleVO> simpleVOS=new ArrayList<>();
    private List<AnimationSimpleDO> simpleDOS=new ArrayList<>();
    private List<CategoryDO> categoryDOS=new ArrayList<>();

    @Override
    public AnimationDetailedVO getAnimationDetailsVO(Integer aid) {
        aSD=getAnimationSimpleDO(aid);
        aDD=getAnimationDetailedDO(aid);
        cD=getCategoryDO(aSD.getCid());
        list=getCommentsDOList(aid);
        aDV.setAnimationSimple(aSD);
        aDV.setAnimationDatailed(aDD);
        aDV.setCategory(cD);
        aDV.setComments(list);
        return aDV;
    }

    /**
     * 获取推荐数据
     * @author zqm
     * @param limit
     * @return
     */
    @Override
    public List<AnimationSimpleVO> listRecommended(LimitUtil limit) {
        long len = animationDao.countAnimationSimple();
        if (len == 0) {
            return null;
        }
        limit.init(len, 4, 1);
        List<AnimationSimpleDO> simpleDOList = animationDao.listAnimationSimple(limit.getSelect(), limit.getNum());
        if (simpleDOList == null) {
            return null;
        }
        List<AnimationSimpleVO> voList = new ArrayList<>(simpleDOList.size());
        for (AnimationSimpleDO item : simpleDOList) {
            AnimationSimpleVO avo = new AnimationSimpleVO();
            avo.setAnimationSimple(item);
            avo.setCategory(getCategoryDO(item.getCid()));
            voList.add(avo);
        }
        return voList;
    }

    @Override
    public List<AnimationSimpleDO> getAnimationMore(Integer aid) {
        Integer categoryId= getAnimationSimpleDO(aid).getCid();
        return animationDao.getAnimationMore(categoryId);
    }

    @Override
    public List<AnimationSimpleVO> getAllAnimationSimpleVO() {
        simpleVOS.clear();
        simpleDOS=animationDao.getAllAnimationSimpleDO();
        categoryDOS=animationDao.getAllCategory();
        for(int i=0;i<simpleDOS.size();i++){
            AnimationSimpleVO aVO = new AnimationSimpleVO();
            aVO.setAnimationSimple(simpleDOS.get(i));
            aVO.setCategory(categoryDOS.get(i));
            simpleVOS.add(aVO);
        }
        return simpleVOS;
    }

    /**
     * 获取最近更新数据
     * @author zqm
     * @param limit
     * @return
     */
    @Override
    public List<AnimationSimpleVO> listRecently(LimitUtil limit) {
        String time = RecentlyUtil.getRecentlyTimeStr(1);
        long len = animationDao.countAnimationSimpleRecently(time);
        if (len == 0) {
            return null;
        }
        limit.init(len, 4, 1);
        List<AnimationSimpleDO> simpleDOList = animationDao.listAnimationSimpleRecently(time,limit.getSelect(),
                limit.getNum());
        List<AnimationSimpleVO> voList = new ArrayList<>(simpleDOList.size());
        for (AnimationSimpleDO item : simpleDOList) {
            AnimationSimpleVO avo = new AnimationSimpleVO();
            avo.setAnimationSimple(item);
            avo.setCategory(getCategoryDO(item.getCid()));
            voList.add(avo);
        }
        return voList;
    }

    /**
     * 更新推荐和最近更新的动漫数据
     * @author zqm
     * @param limit
     * @return
     */
    @Override
    public List<AnimationSimpleVO> flashIndexContext(String model, LimitUtil limit) {
        List<AnimationSimpleVO> list = new ArrayList<>(limit.getNum());
        List<AnimationSimpleDO> ados = null;
        // 推荐模块
        if (model.equals("RecommendedLimit")) {
            ados = animationDao.listAnimationSimple(limit.getSelect(), limit.getNum());
        }
        // 最近更新模块
        if (model.equals("RecentlyLimit")) {
            ados = animationDao.listAnimationSimpleRecently(RecentlyUtil.getRecentlyTimeStr(1),
                    limit.getSelect(), limit.getNum());
        }
        for (AnimationSimpleDO item : ados) {
            AnimationSimpleVO avo = new AnimationSimpleVO();
            avo.setAnimationSimple(item);
            avo.setCategory(getCategoryDO(item.getCid()));
            list.add(avo);
        }
        return list;
    }

    /**
     * 获取每周放送数据
     * @author zqm
     * @param week
     * @return
     */
    @Override
    public List<AnimationSimpleVO> listFlash(String week) {
        List<AnimationSimpleDO> simpleDOList = animationDao.listAnimationSimpleByFlashTime(week, 0, 7);
        if (simpleDOList == null) {
            return null;
        }
        List<AnimationSimpleVO> voList = new ArrayList<>(simpleDOList.size());
        for (AnimationSimpleDO item : simpleDOList) {
            AnimationSimpleVO avo = new AnimationSimpleVO();
            avo.setAnimationSimple(item);
            avo.setCategory(getCategoryDO(item.getCid()));
            voList.add(avo);
        }
        return voList;
    }

    // 排行榜用
    @Override
    public List<AnimationSimpleVO> listSearch(SearchMessage search) {
        String sql = "select * from animation_simple where 1=1 ";
        if (search.getAname() != null) {
            sql = sql + "and aname like " + search.getAname() + "% limit ?,?";
            return null;
        }
        if (search.getCid() != null) {
            sql = sql + "and cid=" + search.getCid() + " ";
        }
        if (search.getState() != null) {
            sql = sql + "and state='" + search.getState() + "' ";
        }
        if (search.getYear() != null) {
            sql = sql + "and create_time between '" + search.getYear() + "-01-01 00:00:00' and '" + search.getYear() + "-12-31 23:59:59' ";
        }
        if (search.getQuarter() != null) {
            sql = sql + "and QUARTER(create_time)=" + search.getQuarter() + " ";
        }
        if (search.getSort() != null) {
            if (search.getSort().equals("更新时间")){
                sql = sql + "order by create_time desc ";
            } else if (search.getSort().equals("热度")){
                sql = sql + "order by people_num desc ";
            } else if (search.getSort().equals("评分")){
                sql = sql + "order by score desc ";
            }
        }
        sql = sql + "limit ?,?";
        List<AnimationSimpleVO> list = new ArrayList<>();
        List<AnimationSimpleDO> dos = animationDao.listAnimationSimpleBySearch(sql, 0, 60);
        if (dos == null) {
            return list;
        }
        for (AnimationSimpleDO item : dos) {
            AnimationSimpleVO avo = new AnimationSimpleVO();
            avo.setAnimationSimple(item);
            avo.setCategory(getCategoryDO(item.getCid()));
            list.add(avo);
        }
        return list;
    }

    public List<AnimationSimpleVO> listSearchJump(SearchMessage search) {
        return null;
    }

    // 目录页用
    @Override
    public List<AnimationSimpleVO> listSearch(SearchMessage search, LimitUtil limit) {
        String sql = "select * from animation_simple where 1=1 ";
        if (search.getAname() != null) {
            String aname = search.getAname() + "%";
            sql = sql + "and aname like '" + aname + "' ";
//            sql = sql + "and aname like " + search.getAname() + "% limit ?,?";
//            return null;
        }
        if (search.getCid() != null) {
            sql = sql + "and cid=" + search.getCid() + " ";
        }
        if (search.getState() != null) {
            sql = sql + "and state='" + search.getState() + "' ";
        }
        if (search.getYear() != null) {
            sql = sql + "and create_time between '" + search.getYear() + "-01-01 00:00:00' and '" + search.getYear() + "-12-31 23:59:59' ";
        }
        if (search.getQuarter() != null) {
            sql = sql + "and QUARTER(create_time)=" + search.getQuarter() + " ";
        }
        if (limit.getTotal() == null) {
            String countSql = sql.replace("*", "count(*)");
            //System.out.println(countSql);
            long len = animationDao.countAnimationSimpleBySearch(countSql);
            // limit.init(len, 10, 1);
            limit.init(len, 3, 1);// 测试
        }
        if (search.getSort() != null) {
            if (search.getSort().equals("更新时间")){
                sql = sql + "order by create_time desc ";
            } else if (search.getSort().equals("热度")){
                sql = sql + "order by people_num desc ";
            } else if (search.getSort().equals("评分")){
                sql = sql + "order by score desc ";
            }
        }
        sql = sql + "limit ?,?";
        List<AnimationSimpleVO> list = new ArrayList<>();
        List<AnimationSimpleDO> dos = animationDao.listAnimationSimpleBySearch(sql, limit.getSelect(), limit.getNum());
        if (dos == null) {
            return list;
        }
        for (AnimationSimpleDO item : dos) {
            AnimationSimpleVO avo = new AnimationSimpleVO();
            avo.setAnimationSimple(item);
            avo.setCategory(getCategoryDO(item.getCid()));
            list.add(avo);
        }
        return list;
    }

    @Override
    public boolean addCollectAnimation(Integer aid,Integer uid) {
        return animationDao.addCollect(aid,uid);
    }

    @Override
    public boolean removeCollectAnimation(Integer aid,Integer uid) {
        return animationDao.removeCollect(aid,uid);
    }

    @Override
    public boolean findCollectAnimation(Integer aid, Integer uid) {
        return animationDao.findCollect(aid,uid);
    }

    @Override
    public List<AnimationDetailedVO> listAnimationDetailed(LimitUtil limit, String search) {
        if (limit.getTotal() == null) {
            if (search == null || search.equals("")) {
                limit.init(animationDao.countAnimationSimple(), 3, 1);// 测试
            } else {
                search = search + "%";
                limit.init(animationDao.countAnimationSimple(search), 3, 1);// 测试
            }
        }
        List<AnimationSimpleDO> aso = null;
        if (search == null || search.equals("")) {
            aso = animationDao.listAnimationSimple(limit.getSelect(), limit.getNum());
        } else {
            aso = animationDao.listAnimationSimple(search, limit.getSelect(), limit.getNum());
        }
        if (aso == null) {
            return null;
        }
        List<AnimationDetailedVO> list = new ArrayList<>(aso.size());
        for (int i = 0; i < aso.size(); i++) {
            AnimationDetailedVO avo = new AnimationDetailedVO();
            avo.setAnimationSimple(aso.get(i));
            avo.setCategory(animationDao.getCategory(aso.get(i).getCid()));
            avo.setAnimationDatailed(animationDao.getAnimationDetailed(aso.get(i).getAid()));
            list.add(avo);
        }
        return list;
    }

    @Override
    public AnimationDetailedVO getAnimationDetailed(Integer aid) {
        AnimationDetailedVO adv = new AnimationDetailedVO();
        adv.setAnimationSimple(animationDao.getAnimationSimple(aid));
        adv.setAnimationDatailed(animationDao.getAnimationDetailed(aid));
        adv.setCategory(animationDao.getCategory(adv.getAnimationSimple().getCid()));
        return adv;
    }

    @Override
    public boolean updateAnimation(AnimationDetailedVO animationDetailedVO) {
        int simple = animationDao.updateAnimationSimple(animationDetailedVO.getAnimationSimple());
        int detail = animationDao.updateAnimationDetail(animationDetailedVO.getAnimationDatailed());
        if (simple == 1 || detail == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertAnimation(AnimationDetailedVO animationDetailedVO) {
        int simple = animationDao.insertAnimationSimple(animationDetailedVO.getAnimationSimple());
        animationDetailedVO.getAnimationDatailed().setAid(animationDao.getAidByAname(
                animationDetailedVO.getAnimationSimple()));
        int detail = animationDao.insertAnimationDetail(animationDetailedVO.getAnimationDatailed());
        if (simple == 1 || detail == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteAnimation(int aid) {
        int comment = commentDao.deleteCommentByAid(aid);
        int collect = collectDao.deleteCollectByAid(aid);
        int detail = animationDao.deleteAnimationDetailByAid(aid);
        int simple = animationDao.deleteAnimationSimpleByAid(aid);
        if (simple == 1 && detail == 1) {
            return true;
        }
        return false;
    }


    private AnimationSimpleDO getAnimationSimpleDO(Integer aid){
        return animationDao.getAnimationSimple(aid);
    }
    private AnimationDetailedDO getAnimationDetailedDO(Integer aid){
        return animationDao.getAnimationDetailed(aid);
    }
    private CategoryDO getCategoryDO(Integer cid){
        return animationDao.getCategory(cid);
    }
    private List<CommentsDO> getCommentsDOList(Integer aid){
        return animationDao.getCommentsList(aid);
    }
}
